﻿--[[
   @brief    tray_tips_control
   @author   zhjyang
   @version  1.0
   @date     2015/07/27
--]]


tray_tips_control = 
{
    init = function()
        base_utility.log("[tray_tips_control] init");
        
        ierd_tgp.tpf_ui.Require("tray_tips\\model.lua");
        ierd_tgp.tpf_ui.Require("tray_tips\\view.lua");
        ierd_tgp.tpf_ui.Require("TPFLocalization.lua");
        
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui",
                "ierd_tgp.tpf_ui.GetTPFUI();");
        -- tpfui:AddLuaListener(ierd_tgp.game_library.K_PREUPDATE_BEGIN, 
        --     "tray_tips_control.on_predownload_begins");
        -- tpfui:AddLuaListener(ierd_tgp.game_library.K_PREUPDATE_RESULT, 
        --     "tray_tips_control.on_predownload_ends");
        tpfui:AddLuaListener(ierd_tgp.tray_ui.ON_ICON_TIMEOUT, 
            "tray_tips_control.on_icon_timeout");
        tpfui:AddLuaListener(ierd_tgp.tray_ui.ON_ICON_SHOWN, 
            "tray_tips_control.on_icon_shown");
    end;
    
    on_predownload_begins = function(event_id, json_str)
        base_utility.log("[tray_tips_control] on_predownload_begins");
        --local info = base_utility.json_to_table(json_str);
        tray_tips_control.is_multi_preupdate(true);
        tray_tips_view.show(true);
    end;
    
    on_predownload_ends = function(event_id, json_str)
        base_utility.log("[tray_tips_control] on_predownload_ends");
        local info = base_utility.json_to_table(json_str);
        tray_tips_control.set_tips(info.game_id, info.result + 1, 3000);
        if tray_tips_control.is_multi_preupdate(false) then
            tray_tips_view.show(true);
        else
            tray_tips_view.show(false);
        end;
    end;
    
    on_icon_shown = function(event_id, json_str)
        base_utility.log("[tray_tips_control] on_icon_shown");
        local info = base_utility.json_to_table(json_str);
        tray_tips_view.show_tips(info.x, info.y);  
    end;
    
    on_icon_timeout = function(event_id, json_str)
        base_utility.log("[tray_tips_control] on_icon_timeout");
        tray_tips_control.is_multi_preupdate(true);
        local pos = ierd_tgp.tray_ui.Point:new();
        base_utility.get_component_adapter("ITray_ui","ierd_tgp.tray_ui.get_Tray_ui_adapter()"):get_cursor_pos(pos);
        local rc = tray_tips_view.tips_dialog:GetRectEx(TRUE,TRUE);
        local x, y = pos.x, pos.y;
        pos:delete();
        if x < rc.left or x > rc.right or y < rc.top or y > rc.bottom then
            tray_tips_view.hide();
        end;
    end;
    
    is_multi_preupdate = function(set)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        local count = 0;
        local game_list = ierd_tgp.game_library.Game_list:new();
        game_lib:get_visible_games(game_list);
        local game_state = ierd_tgp.game_library.GameState:new();
        for i = 0, game_list.count - 1 do
            local game_id = game_list.game_id_list[i];
            game_lib:get_game_state(game_id, game_state);
            if game_state.iPre_download == 1 then
                if game_state.pre_download_status == ierd_tgp.game_library.kWaitting or
                    game_state.pre_download_status == ierd_tgp.game_library.kDownloading then
                    if set and count == 0 then
                        tray_tips_control.set_tips(game_id, 0, 5000);
                    end;
                    count = count + 1;
                    if count > 1 then
                        break;
                    end;
                end;
            end;
        end;
        game_state:delete();
        game_list:delete();
        if count > 1 then
            return true;
        else
            return false;
        end;
    end;
    
    set_tips = function(game_id, status, timeout)
        local game_lib =
            base_utility.get_component_adapter("IGame_library",
                "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib == nil then
            return;
        end;
        local game_info = ierd_tgp.game_library.GameDetailInfo:new();
        game_lib:get_game_detail_info(game_id, game_info);
        tray_tips_model.game_name = game_info.name;
        game_info:delete();
        tray_tips_model.timeout = timeout;
        local tips = LoadStr(ID_TIPS_UPDATEDOWN);
        if status == 0 then
            tray_tips_model.download_state = tips..LoadStr(ID_TIPS_ING);
        elseif status == 1 then
            tray_tips_model.download_state = tips..LoadStr(ID_TIPS_DONE);
        elseif status == 2 then
            tray_tips_model.download_state = tips..LoadStr(ID_TIPS_PAUSE);
        elseif status == 3 then
            tray_tips_model.download_state = tips..LoadStr(ID_TIPS_FAILED);
        else
            tray_tips_model.download_state = tips..LoadStr(ID_TIPS_NOTSTART);
        end;
    end;
};

tray_tips_control.init();